#include <zmq.h>
#include "stdio.h"
#include <unistd.h>
#include <assert.h>
#include "log.h"

int main(int argc, char * argv[]) {
    printf("i need sub\n");

    char szData[64] = { 0 };
    snprintf(szData, sizeof(szData), "sub_%d", getpid());
    init_log(szData);
    set_log_level(7);

    void* context = zmq_ctx_new();
    assert(context != NULL);

    int ret = zmq_ctx_set(context, ZMQ_MAX_SOCKETS, 1);
    assert(ret == 0);

    void* subscriber = zmq_socket(context, ZMQ_SUB);
    assert(subscriber != NULL);

    ret = zmq_connect(subscriber, "tcp://127.0.0.1:9999");
    assert(ret == 0);

    ret = zmq_setsockopt(subscriber, ZMQ_SUBSCRIBE, "", 0);
    assert(ret == 0);

    char buf[16] = { 0 };
    while (1) {
        ret = zmq_recv(subscriber, buf, 16, ZMQ_DONTWAIT);
        if (ret != -1) {
            buf[ret] = 0;
            log_debug("message:%s\n", buf);
        }
        usleep(1);
    }
    return 0;
}
